﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using FX.CC.Andon.Entity.BasicDataManage;
using FX.CC.Andon.Service.BasicDataManage;
using FX.CC.Andon.Utility.Json;

namespace FX.CC.Andon.Business.BasicDataManage
{
    public class ReqHelpBusiness
    {
        private readonly ReqHelpService _dal = new ReqHelpService();


        public string GetReqHelpList(ReqHelpQcEntity qcmodel)
        {
            var total = _dal.GetReqHelpCount(qcmodel);
            var list = _dal.GetReqHelpList(qcmodel);
            return JsonHelper.Encode(total, list);
        }

        public void AddReqHelp(string data)
        {
            var model = new ReqHelpEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            var list = new List<ReqHelpEntity>();
            foreach (Hashtable row in rows)
            {
                model.WSID = new Guid(row["WSID"].ToString());
                model.ETypeCode = row["ETypeCode"].ToString();
                model.ArrList = (ArrayList)row["arrList"];
                list.AddRange(from Hashtable item in model.ArrList
                              select new ReqHelpEntity
                              {
                                  ReqID = Guid.NewGuid(),
                                  WSID = model.WSID,
                                  ETypeCode = model.ETypeCode,
                                  UserID = new Guid(item["UserID"].ToString())
                              });
            }
            _dal.AddReqHelp(list);
        }

        public void ModifyReqHelp(string data)
        {
            var model = new ReqHelpEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            var list = new List<ReqHelpEntity>();
            foreach (Hashtable row in rows)
            {
                model.WSID = new Guid(row["WSID"].ToString());
                model.ETypeCode = row["ETypeCode"].ToString();
                model.ArrList = (ArrayList)row["arrList"];
                list.AddRange(from Hashtable item in model.ArrList
                              select new ReqHelpEntity
                              {
                                  ReqID = Guid.NewGuid(),
                                  WSID = model.WSID,
                                  ETypeCode = model.ETypeCode,
                                  UserID = new Guid(item["UserID"].ToString())
                              });
            }
            _dal.RemoveReqHelp(model.WSID, model.ETypeCode);

            _dal.AddReqHelp(list);

        }

        public bool RemoveReqHelp(string id)
        {
            return _dal.RemoveReqHelp(id);
        }

        public string GetReqHelpUsers(ReqHelpQcEntity qcmodel)
        {
            var list = _dal.GetReqHelpUsers(qcmodel);
            return JsonHelper.Encode(list);
        }
    }
}
